package com.atnjupt.web;

import com.atnjupt.bean.Book;
import com.atnjupt.bean.Cart;
import com.atnjupt.bean.CartItem;
import com.atnjupt.service.impl.BookServiceImpl;
import com.atnjupt.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Wizarder
 * @create 2021-06-10 17:26
 */
public class CartServlet extends BaseServlet {
    private BookServiceImpl bookService = new BookServiceImpl();

    protected void ajaxAddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取添加到购物车的图书id
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //通过bookService中的bookService.queryBookById()方法查询到要添加的Book对象
        Book book = bookService.queryBookById(id);
        //把这个Book对象转换为CartItem对象
        CartItem cartItem  = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //不能new一个cart,这样每次添加都相当于添加到一个cart中，因此需要把cart添加到session的域中
        //把最后添加的图书存入session域，首页回显
        request.getSession().setAttribute("lastAddBook",cartItem.getName());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            //把cart添加到session域中
            request.getSession().setAttribute("cart",cart);
        }
        //把cartItem这个对象添加到cart对象中
        cart.addItem(cartItem);
        //请求头Referer,它存储了求发起时的地址，通过这个请求头可以把请求时候的地址发送给浏览器
//        String referer = request.getHeader("Referer");
//        System.out.println("当前请求的地址:" + referer);
        //请求重定向到添加的页面
//        response.sendRedirect(referer);
        //用ajax回显购物车添加的图书和添加数量
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("lastAddBook",cartItem.getName());
        resultMap.put("totalCount",cart.getTotalCount());

        Gson gson = new Gson();
        String mapJsonString = gson.toJson(resultMap);

        response.getWriter().write(mapJsonString);

    }
    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取添加到购物车的图书id
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //通过bookService中的bookService.queryBookById()方法查询到要添加的Book对象
        Book book = bookService.queryBookById(id);
        //把这个Book对象转换为CartItem对象
        CartItem cartItem  = new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        //不能new一个cart,这样每次添加都相当于添加到一个cart中，因此需要把cart添加到session的域中
        //把最后添加的图书存入session域，首页回显
        request.getSession().setAttribute("lastAddBook",cartItem.getName());
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            //把cart添加到session域中
            request.getSession().setAttribute("cart",cart);
        }
        //把cartItem这个对象添加到cart对象中
        cart.addItem(cartItem);
        //请求头Referer,它存储了求发起时的地址，通过这个请求头可以把请求时候的地址发送给浏览器
        String referer = request.getHeader("Referer");
//        System.out.println("当前请求的地址:" + referer);
        //请求重定向到添加的页面
        response.sendRedirect(referer);
    }
    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取删除的id值
        int id = WebUtils.parseInt(request.getParameter("id"), 0);
        //调用session域中的cart
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (cart != null){
            //调用方法删除对应id的图书
            cart.deleteItem(id);
            //请求重定向到删除的页面
            response.sendRedirect(request.getHeader("referer"));
        }
    }
    protected void clearCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //从session中获取cart对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            //调用clear方法清空购物车
            cart.clear();
            //重新回到请空的页面
            response.sendRedirect(request.getHeader("referer"));
        }
    }
    protected void updateItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //获取请求发送的bookId和修改的数量Count
        int bookId = WebUtils.parseInt(request.getParameter("bookId"),0);
        int count = WebUtils.parseInt(request.getParameter("count"), 1);
        //获取session中的cart对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if(cart != null){
            cart.updateItem(bookId,count);
            //重新定向到请求的页面
            response.sendRedirect(request.getHeader("referer"));
        }

    }
}
